<?php
/**
 * RelationsGenerator
 * @author Juan Carlos Jarquin
 */

require_once "application/lib/generators/php/default/BaseGenerator.php";

/**
 * Clase que genera los Collections
 */
class RelationsGenerator extends BaseGenerator implements CodeGenerable
{
  
  /**
   * Donde se va a guardar el archivo
   */
  protected $fileName = "application/temporal/%sRelations.php";

  /**
   * @var mixed $requiredFlags
   */    
  public $requiredFlags = 'create-relations';

  /**
   * run
   */
  public function run()
  {
  	$this->view->table = $this->table;
  	
  	$foreignKeys = new BenderFieldCollection();
  	foreach ($this->table->getFields() as $field){
  		/* @var $field BenderField */
  	    if( $this->isForeignKey($field) ){
                $foreigObject = str_replace('id_', '', $field->getName());
                $field->setForeignObject(Formatter::underScoreToUpperCamelCase($foreigObject));
                $field->setForeignLowerObject(Formatter::underScoreToCamelCase($foreigObject));
                $foreignKeys->append($field);
        }	    
  	}
  	
  	//Formatter::camelCaseToUpperCase();
  	$this->formatter = new Formatter();
    $this->view->foreignKeys = $foreignKeys;
    $this->view->foreignKeysClone = clone $foreignKeys;    
    $this->view->nonForeignKeys = $this->table->getNonForeignKeys();
  }
  
    /**
     * Es una llave foranea?
     * @param BenderField $field
     * @return boolean
     */
    public function isForeignKey(BenderField $field)
    {
        return preg_match('(^id\_|\_id$)',$field->getName());
    }
  
  

  
}
